//
//  PublicUtil.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/19.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "IKPublicUtil.h"

@implementation IKPublicUtil

+(NSString *)currentECGDataFileDir{
    return [IKPublicUtil documentsDir:@"ecgdata"];
}

+(NSString *)documentsDir:(NSString *)dir{
    NSString* home = NSHomeDirectory();
    NSString* path = [home stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",dir]];
    [IKPublicUtil checkFileDir:path];
    return path;
}

+(BOOL)checkFileDir:(NSString *)fileDir{
    NSFileManager* manager= [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:fileDir]) {
        return [manager createDirectoryAtPath:fileDir withIntermediateDirectories:NO attributes:nil error:nil];
    }
    return YES;
}

+(NSString *)dir:(NSString *)dir file:(NSString *)file{
    if ([dir hasSuffix:@"/"] == NO) {
        dir = [dir stringByAppendingString:@"/"];
    }
    return [NSString stringWithFormat:@"%@%@",dir,file];
}

+(void)runOnMainThread:(void (^)())run{
    if ([NSThread currentThread].isMainThread) {
        if(run)run();
    }else{
        dispatch_async( dispatch_get_main_queue() ,  ^(void) {
            if(run)run();
        });
    }
}

+(BOOL)isEmpty:(NSString *)str{
    if (!str) {
        return true;
    } else
    {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0)
        {
            return true;
        } else
        {
            return false;
        }
    }
}

+ (CGFloat) heightForString:(NSString *)value fontSize:(CGFloat)fontSize andWidth:(CGFloat)width{
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]};
    CGSize size= [value boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return size.height + 10;
}

+ (CGFloat) widthForString:(NSString *)value fontSize:(CGFloat)fontSize
{
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]};
    CGSize size= [value boundingRectWithSize:CGSizeMake(KScreenW, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return size.width + 4;
    
}
+(void)changeUIInterfaceOrientationPortrait{
    if ([UIDevice currentDevice].orientation != UIInterfaceOrientationPortrait) {
        [IKPublicUtil changeUIInterfaceOrientation:UIInterfaceOrientationPortrait];
    }
}

+(void)changeUIInterfaceOrientation:(UIInterfaceOrientation)orientation{
    [IKPublicUtil runOnMainThread:^{
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            NSNumber *value = [NSNumber numberWithInt:(int)orientation];
            [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        }
    }];
}

+(CGFloat) mm2pt:(CGFloat)mm{
    return mm * [self dpi] / 25.4  / [UIScreen mainScreen].scale;
}
+(CGFloat)pt2mm:(CGFloat)pt{
    return pt * [UIScreen mainScreen].scale / [self dpi]  * 24;
}

+(CGFloat) dpi {
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat dpi  = 1.0;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        dpi = 132 * scale;
    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if (scale >= 3) {
            dpi = 133.6667 * scale;
        }else{
            dpi = 163 * scale;
        }
    } else {
        dpi = 160 * scale;
    }
    return dpi;
}


+(UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
